﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLT_BLL;
using QLT_Public;


namespace GiaoDien
{
    public partial class uc_HoaDonBH : DevExpress.XtraEditors.XtraUserControl
    {
        public uc_HoaDonBH()
        {
            InitializeComponent();
        }
        public void Huy_Text(Control ctr)
        {
            if (ctr is TextBox || ctr is TextEdit)
            {
                ctr.Text = string.Empty;
            }
            foreach (Control i in ctr.Controls)
                Huy_Text(i);
        }
        public void GetData()
        {
            string sql = "select * from tbl_KhachHang,tbl_HoaDonBH where tbl_HoaDonBH.MaKH=tbl_KhachHang.MaKH and tbl_HoaDonBH.MaHDBH='" + txtMaHDBH.Text + "' and tbl_KhachHang.TenKH=N'" + cmbKH.Text + "'";
            DataTable dt = new DataTable();
            dt = hoadonbh_bll.taoBang(sql);
            cmbMaKH.DataSource = dt;
            cmbMaKH.DisplayMember = "MaKH";

            string sql1 = "select * from tbl_NhanVien,tbl_HoaDonBH where tbl_HoaDonBH.MaNV=tbl_NhanVien.MaNV and tbl_HoaDonBH.MaHDBH='" + txtMaHDBH.Text + "' and tbl_NhanVien.HoTen=N'" + cmbNV.Text + "'";
            DataTable dt1 = new DataTable();
            dt1 = hoadonbh_bll.taoBang(sql1);
            cmbMaNV.DataSource = dt1;
            cmbMaNV.DisplayMember = "MaNV";
            cmbNV.DataSource = dt1;
            cmbNV.DisplayMember = "HoTen";

            string sql2 = "select * from tbl_KhachHang,tbl_HoaDonBH where tbl_HoaDonBH.MaKH=tbl_KhachHang.MaKH and tbl_HoaDonBH.MaHDBH='" + txtMaHDBH.Text + "' and tbl_KhachHang.DiaChi=N'" + cmbDiaChi.Text + "'";
            DataTable dt2 = new DataTable();
            dt2 = hoadonbh_bll.taoBang(sql2);
            cmbMaKH.DataSource = dt2;
            cmbMaKH.DisplayMember = "MaKH";
            cmbDiaChi.DataSource = dt2;
            cmbDiaChi.DisplayMember = "DiaChi";
        }
        private void uc_HoaDonBH_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = hoadonbh_bll.LayDuLieu();
            bbi_Luu.Enabled = false;
            txtMaHDBH.Enabled = false;
            txtTenHD.Enabled = false ;
            dateTimeNgayLap.Enabled = false;
            cmbKH.Enabled = false;
            cmbDiaChi.Enabled = false;
            cmbNV.Enabled = false;
            btnThem_KH.Enabled = false;
            bbi_Huy.Enabled = false;
            cmbMaKH.Hide();
            cmbMaNV.Hide();
        }
        int Flag;
        private void bbi_Them_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Flag = 0;
            Huy_Text(this);
            bbi_Them.Enabled = false;
            bbi_Sua.Enabled = false;
            bbi_Luu.Enabled = true;
            txtMaHDBH.Enabled = false;
            txtTenHD.Enabled = true;
            dateTimeNgayLap.Enabled = true;
            cmbKH.Enabled = true;
            cmbDiaChi.Enabled = true;
            cmbNV.Enabled = true;
            btnThem_KH.Enabled = true;
            bbi_Huy.Enabled = true;
        }

        private void bbi_Sua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Flag = 1;
            txtMaHDBH.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Hóa Đơn Bán").ToString();
            txtTenHD.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Hóa Đơn").ToString();
            dateTimeNgayLap.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Ngày Lập").ToString();
            cmbKH.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Khách Hàng").ToString();
            cmbDiaChi.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Địa Chỉ").ToString();
            cmbNV.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Nhân Viên").ToString();
            GetData();

            bbi_Them.Enabled = false;
            bbi_Sua.Enabled = false;
            bbi_Luu.Enabled = true;
            txtMaHDBH.Enabled = false;
            txtTenHD.Enabled = true;
            dateTimeNgayLap.Enabled = true;
            cmbKH.Enabled = true;
            cmbDiaChi.Enabled = true;
            cmbNV.Enabled = true;
            btnThem_KH.Enabled = true;
            bbi_Huy.Enabled = true;
        }
        NhatKy_Bll nhatKy_bll = new NhatKy_Bll();
        HoaDonBH_BLL hoadonbh_bll = new HoaDonBH_BLL();
        private void bbi_Luu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            NhatKy_Public nk = new NhatKy_Public();
            HoaDonBH_Public hd = new HoaDonBH_Public();
            if (txtTenHD.Text != "" && cmbKH.Text != "" && cmbDiaChi.Text != "" && cmbNV.Text != "")
            {
                if (Flag == 0)
                {
                    try
                    {
                        hd.TenHD = txtTenHD.Text;
                        hd.NgayLap = Convert.ToDateTime(dateTimeNgayLap.Text);
                        hd.MaKH = int.Parse(cmbMaKH.Text);
                        hd.MaNV = int.Parse(cmbMaNV.Text);
                        hoadonbh_bll.insert_HoaDonBH(hd, hd, hd, hd);
                        string d = DateTime.Now.ToString();
                        Class_TrungGian tg = new Class_TrungGian();
                        nk.TaiKhoan = tg.TaiKhoan;
                        nk.NgayGio = d;
                        nk.DoiTuong = tg.DoiTuong;
                        nk.HanhDong = "Thêm Mới";
                        nk.MoTa = "Thêm mới Hóa đơn bán hàng: '" + txtTenHD.Text + "'";
                        nhatKy_bll.insert_NhatKy(nk, nk, nk, nk, nk);
                        XtraMessageBox.Show("Thêm mới thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Huy_Text(this);
                        bbi_Huy.Enabled = false;
                        cmbKH.ResetText();
                        cmbDiaChi.ResetText();
                        cmbNV.ResetText();
                    }

                    catch (Exception)
                    {
                        XtraMessageBox.Show("Thực hiện không thành công!Bạn hãy nhập lại dữ liệu", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }

                if (Flag == 1)
                {
                    try
                    {

                        hd.MaHDBH = int.Parse(txtMaHDBH.Text);
                        hd.TenHD = txtTenHD.Text;
                        hd.NgayLap = Convert.ToDateTime(dateTimeNgayLap.Text);
                        hd.MaKH = int.Parse(cmbMaKH.Text);
                        hd.MaNV = int.Parse(cmbMaNV.Text);
                        hoadonbh_bll.update_HoaDonBH(hd, hd, hd, hd, hd);
                        string d = DateTime.Now.ToString();
                        Class_TrungGian tg = new Class_TrungGian();
                        nk.TaiKhoan = tg.TaiKhoan;
                        nk.NgayGio = d;
                        nk.DoiTuong = tg.DoiTuong;
                        nk.HanhDong = "Sửa";
                        nk.MoTa = "Sửa thông tin Mã Hóa đơn bán hàng: '" + txtMaHDBH.Text + "' có tên: '"+txtTenHD.Text+"'";
                        nhatKy_bll.insert_NhatKy(nk, nk, nk, nk, nk);
                        XtraMessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Huy_Text(this);
                        bbi_Huy.Enabled = false;
                        cmbKH.ResetText();
                        cmbDiaChi.ResetText();
                        cmbNV.ResetText();                   
                    }

                    catch (Exception)
                    {
                        XtraMessageBox.Show("Thực hiện không thành công!Bạn hãy nhập lại dữ liệu", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            else
            {
                XtraMessageBox.Show("Bạn chưa nhập đủ thông tin cần thiết!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            uc_HoaDonBH_Load(sender, e);
            bbi_Them.Enabled = true;
            bbi_Sua.Enabled = true;
            bbi_Luu.Enabled = false;
            txtMaHDBH.Enabled = false;
            txtTenHD.Enabled = false;
            dateTimeNgayLap.Enabled = false;
            cmbKH.Enabled = false;
            cmbDiaChi.Enabled = false;
            cmbNV.Enabled = false;
            btnThem_KH.Enabled = false;
            
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            txtMaHDBH.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Hóa Đơn Bán").ToString();
            txtTenHD.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Hóa Đơn").ToString();
            dateTimeNgayLap.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Ngày Lập").ToString();
            cmbKH.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Khách Hàng").ToString();
            cmbDiaChi.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Địa Chỉ").ToString();
            cmbNV.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Nhân Viên").ToString();
            GetData();
        }

        private void bbi_Xoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            
            HoaDonBH_Public hd = new HoaDonBH_Public();
            hd.MaHDBH = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Hóa Đơn Bán").ToString());
            hd.TenHD= gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Hóa Đơn").ToString();//tên khách hàng là kiểu chuỗi 'âsâsa' mà hd.MaKh lại là kiểu int làm sao converet t
            if (XtraMessageBox.Show("Bạn có muốn xóa Mã hóa đơn: '" + hd.MaHDBH + "' của Tên hóa đơn: '" + hd.TenHD + "'này không?", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                NhatKy_Public nk = new NhatKy_Public();
                string d = DateTime.Now.ToString();
                Class_TrungGian tg = new Class_TrungGian();
                nk.TaiKhoan = tg.TaiKhoan;
                nk.NgayGio = d;
                nk.DoiTuong = tg.DoiTuong;
                nk.HanhDong = "Xóa";
                nk.MoTa = "Xóa thông tin Hóa đơn bán hàng: '" + hd.TenHD + "'";
                nhatKy_bll.insert_NhatKy(nk, nk, nk, nk, nk);
                try
                {

                    hoadonbh_bll.delete_HoaDonBH(hd);
                    uc_HoaDonBH_Load(sender, e);
                }
                catch (Exception) { }
            }
        }

        private void bbi_Huy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Huy_Text(this);
            bbi_Them.Enabled = true;
            bbi_Sua.Enabled = true;
            bbi_Luu.Enabled = false;
            txtMaHDBH.Enabled = false;
            txtTenHD.Enabled = false;
            dateTimeNgayLap.Enabled = false;
            cmbKH.Enabled = false;
            cmbDiaChi.Enabled = false;
            cmbNV.Enabled = false;
            btnThem_KH.Enabled = false;
            bbi_Huy.Enabled = false;
            cmbNV.ResetText();
            cmbKH.ResetText();
            cmbDiaChi.ResetText();
        }

        private void cmbKH_Click(object sender, EventArgs e)
        {
            string sql = "select * from tbl_KhachHang";
            DataTable dt = new DataTable();
            dt = hoadonbh_bll.taoBang(sql);
            cmbMaKH.DataSource = dt;
            cmbMaKH.DisplayMember = "MaKH";
            cmbKH.DataSource = dt;
            cmbKH.DisplayMember = "TenKH";
        }
        private void cmbKH_TextChanged(object sender, EventArgs e)
        {
            string sql = "select * from tbl_KhachHang where MaKH='"+cmbMaKH.Text+"'";
            DataTable dt = new DataTable();
            dt = hoadonbh_bll.taoBang(sql);
            cmbDiaChi.DataSource = dt;
            cmbDiaChi.DisplayMember = "DiaChi";
        }

        private void cmbNV_Click(object sender, EventArgs e)
        {
            string sql = "select * from tbl_NhanVien";
            DataTable dt = new DataTable();
            dt = hoadonbh_bll.taoBang(sql);
            cmbMaNV.DataSource = dt;
            cmbMaNV.DisplayMember = "MaNV";
            cmbNV.DataSource = dt;
            cmbNV.DisplayMember = "HoTen";
        }

        private void btnThem_KH_Click(object sender, EventArgs e)
        {
            frm_HoaDonBH_KH f = new frm_HoaDonBH_KH();
            f.GetData = new frm_HoaDonBH_KH.GetInt(GetMaKhachHang);
            f.ShowDialog();
        }
        public void GetMaKhachHang(int ma)
        {
            cmbMaKH.Text = ma.ToString();
        }

        private void cmbMaKH_TextChanged(object sender, EventArgs e)
        {
            try
            {
                string sql = "select * from tbl_KhachHang where MaKH='" + cmbMaKH.Text + "'";
                DataTable dt = new DataTable();
                dt = hoadonbh_bll.taoBang(sql);
                cmbKH.DataSource = dt;
                cmbKH.DisplayMember = "TenKH";
            }
            catch (Exception)
            {
               
            }
        }

        private void txtMaHDBH_Leave(object sender, EventArgs e)
        {
            if (txtMaHDBH.Text == "")
                txtMaHDBH.Focus();
        }

        private void txtTenHD_Leave(object sender, EventArgs e)
        {
            if (txtTenHD.Text == "")
                txtTenHD.Focus();
        }

        private void cmbKH_Leave(object sender, EventArgs e)
        {
            if (cmbKH.Text == "")
                cmbKH.Focus();
        }

        private void cmbDiaChi_Leave(object sender, EventArgs e)
        {
            if (cmbDiaChi.Text == "")
                cmbKH.Focus();
        }

        private void cmbNV_Leave(object sender, EventArgs e)
        {
            if (cmbNV.Text == "")
                cmbNV.Focus();
        }



   }
}
